package net.ocps.tchs.soap.entity;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import net.ocps.tchs.soap.Main;
import net.ocps.tchs.soap.event.collisions.Punch;
import net.ocps.tchs.soap.event.events.JumpEvent;

public class JumpSlime extends Entity {

	public JumpSlime( int... params ) {
		super( params );
		mainEvent = new JumpEvent( );
	}

	public JumpSlime( float x, float y, int width, int height ) {
		super( x, y, width, height ); 
		mainEvent = new JumpEvent( );
	}
	public JumpSlime( float x, float y, int width, int height, String image ) {
		super( x, y, width, height, image ); 
		mainEvent = new JumpEvent( );
	}
	@Override
	public void onCreate() {
		collisionEvent = new Punch( Main.entitys.get( 0 ) );
		batch = new SpriteBatch();
		texture = new Sprite( new Texture(Gdx.files.internal("data/SlimeJump 1.gif")) );
		frame1 = new Sprite( new Texture(Gdx.files.internal("data/SlimeJump 2.gif")));
		frame2 = new Sprite( new Texture(Gdx.files.internal("data/SlimeJump 1.gif")));
		bodyDef = new BodyDef();  
		bodyDef.type = BodyType.DynamicBody;  
		bodyDef.position.set(x+3, y);  
		body = Main.world.createBody(bodyDef);  
		hits = new PolygonShape();  
		if( width <= 0 && height <= 0)
			hits.setAsBox( (texture.getWidth()/2)-20, (texture.getHeight()/2)-12 );
		else if( width > 0 || height > 0 )
			hits.setAsBox( width/2, height/2 );
		fixtureDef = new FixtureDef();  
		fixtureDef.shape = hits;  
		fixtureDef.density = 0.0f;  
		fixtureDef.friction = 1.0f;  
		fixtureDef.restitution = 0.0f;  
		body.createFixture(fixtureDef);  
	}

	@Override
	public void render() {
		try {
			batch.begin();
			{
				x = body.getPosition().x-22;
				y = body.getPosition().y-23;
				batch.setProjectionMatrix( Main.camera.combined );
				if( direction == 1 ) {
					x -= 15;
					texture.flip( true, false );
				}
				else if( direction == -1 )
					texture.flip( false, false );
				if( body.getLinearVelocity().y == 0.0f ) {
					texture.set( frame2 ); 
				} else {
					texture.set( frame1 ); 
				}
				if( direction == 1 )
					texture.flip( true, false );
				else if( direction == -1 )
					texture.flip( false, false );
				if( width <= 0 && height <= 0)
					batch.draw(
							texture, x, y, texture.getOriginX(), texture.getOriginY(), 
							texture.getWidth(), texture.getHeight(), texture.getScaleX(), 
							texture.getScaleY(), (float)Math.toDegrees( body.getAngle() ) );
				else if( width > 0 || height > 0 )
					batch.draw(
							texture, x, y, texture.getOriginX(), texture.getOriginY(), 
							width, height, texture.getScaleX(), texture.getScaleY(), 
							(float)Math.toDegrees( body.getAngle() ) );
				hitbox.set( x, y, texture.getWidth(), texture.getHeight()-7 );
				count ++;
			}
			batch.end();
		} catch( Exception e ) {
			batch.end();
		}
	}

	public Sprite frame1, frame2, frame3;
	int count = 1;
	public BodyDef bodyDef;
	PolygonShape hits;
	FixtureDef fixtureDef;
}
